import 'package:bike_ride/common/mvvm/base_page.dart';
import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:bike_ride/common/widget/info_row_widget.dart';
import 'package:bike_ride/common/widget/page_title_widget.dart';
import 'package:bike_ride/model/protocol/model/contract_m.dart';
import 'package:bike_ride/model/protocol/view_model/protocol_vm.dart';
import 'package:bike_ride/model/web/view/webview_page.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:bike_ride/res/style/color_styles.dart';
import 'package:bike_ride/router/navigator_util.dart';
import 'package:bike_ride/router/routers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

///商户协议
class ProtocolView extends BaseStatefulPage<ProtocolViewModel> {
  const ProtocolView({super.key});

  @override
  ProtocolViewState createState() => ProtocolViewState();
}

class ProtocolViewState
    extends BaseStatefulPageState<ProtocolView, ProtocolViewModel> {
  @override
  void initAttribute() {}

  @override
  void initObserver() {}

  @override
  ProtocolViewModel viewBindingViewModel() {
    return ProtocolViewModel()..viewState = this;
  }

  void notifyChange(){
    if(mounted){
      setState(() {});
    }
  }

  @override
  Widget appBuild(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
        backgroundColor: ColorStyles.color_F3F4F5,
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            PageTitleWidget(title: S.current.merchantAgreement,),
            Expanded(
              child: ListView.builder(
                itemCount: viewModel!.contracts.length,
                padding: EdgeInsets.zero,
                itemBuilder: (context, index) {
                  final item = viewModel!.contracts[index];
                  return _buildItem(item.name??'',item.signed == 1,(){
                    if(item.linkUrl != null){
                      NavigatorUtil.push(context, Routers.webViewPage, arguments: {'url':item.linkUrl,'bottomWidget':_buildBottomButton(item)});
                    }else{
                      NavigatorUtil.push(context, Routers.htmlContent, arguments: {'title':item.name,'content':item.contents,'bottomWidget':_buildBottomButton(item)});
                    }
                  });
                },
              ),
            ),
          ],
        ));
  }

  Widget _buildBottomButton(ContractM item){
    final isSigned = item.signed == 1;
    return HtmlBottomWidget(btnText: isSigned?S.current.cancelSignContract:S.current.toSignContract,onPressed: ()async{
      if(isSigned){
        EasyLoading.showError(S.current.cancelSignTips, duration: const Duration(seconds: 2));
      }else{
        final result = await viewModel!.signContracts(item.id!);
        if(result && mounted){
          viewModel!.getContracts();
          NavigatorUtil.back(context);
        }
      }
    },);
  }

  Widget _buildItem(String title,bool isSign,Function() onPress){
    final signedText = Text(isSign? S.current.signedContract:S.current.noSignContract,style: TextStyle(color: isSign?Theme.of(context).colorScheme.primary:Colors.grey,fontSize: 28.rpx),);
    final arrowForward = Icon(
      Icons.arrow_forward_ios,
      size: 32.rpx,
      color: const Color(0xFF848F99),
    );
    return GestureDetector(
      onTap: onPress,
      child: InfoRowWidget(
        left: title,
        center: signedText,
        right: arrowForward,
      ),
    );
  }
}
